package com.ziJing.multithreading.sync;

import java.util.concurrent.Exchanger;

/**
 * @program Multithreading
 * @description:
 * @author: zhangbo
 * @create: 2022/05/03 18:42
 */
public class T12_TestExchanger {

    static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            String s = "t1";
            try {
                s = exchanger.exchange(s);
                Thread.sleep(1000);
                System.out.println("当前线程：" + Thread.currentThread().getName() + ", s :" + s);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T1").start();



        new Thread(() -> {
            String s = "t2";
            try {
                s = exchanger.exchange(s);
                System.out.println("当前线程：" + Thread.currentThread().getName() + ", s :" + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T2").start();
    }
}
